package com.bobo.apidriver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author bobo
 * @date 2021-04-12
 */
@RestController
public class DriverController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/no/lock/order/{orderId}")
    public String orderNoLock(@PathVariable("orderId") int orderId, int driverId) {
        System.out.println("orderId" + orderId + " driverId:" + driverId);
        String url = "http://service-order/no/lock/grab/" + orderId + "?driverId=" + driverId;
        restTemplate.getForEntity(url,String.class).getBody();
        return "success";
    }

    @GetMapping("/mysql/lock/order/{orderId}")
    public String orderMysqlLock(@PathVariable("orderId") int orderId, int driverId) {
        System.out.println("orderId" + orderId + " driverId:" + driverId);
        String url = "http://service-order/mysql/lock/grab/" + orderId + "?driverId=" + driverId;
        restTemplate.getForEntity(url,String.class).getBody();
        return "success";
    }

    @GetMapping("/redis/lock/order/{orderId}")
    public String orderRedisLock(@PathVariable("orderId") int orderId, int driverId) {
        System.out.println("orderId" + orderId + " driverId:" + driverId);
        String url = "http://service-order/redis/lock/grab/" + orderId + "?driverId=" + driverId;
        restTemplate.getForEntity(url,String.class).getBody();
        return "success";
    }

    @GetMapping("/redisson/lock/order/{orderId}")
    public String orderRedissonLock(@PathVariable("orderId") int orderId, int driverId) {
        System.out.println("orderId" + orderId + " driverId:" + driverId);
        String url = "http://service-order/redisson/lock/grab/" + orderId + "?driverId=" + driverId;
        restTemplate.getForEntity(url,String.class).getBody();
        return "success";
    }

    @GetMapping("/redisson/sentinel/lock/order/{orderId}")
    public String orderRedissonSentinelLock(@PathVariable("orderId") int orderId, int driverId) {
        System.out.println("orderId" + orderId + " driverId:" + driverId);
        String url = "http://service-order/redisson/sentinel/lock/grab/" + orderId + "?driverId=" + driverId;
        restTemplate.getForEntity(url,String.class).getBody();
        return "success";
    }

    @GetMapping("/redisson/red/lock/order/{orderId}")
    public String orderRedissonRedlLock(@PathVariable("orderId") int orderId, int driverId) {
        System.out.println("orderId" + orderId + " driverId:" + driverId);
        String url = "http://service-order/redisson/red/lock/grab/" + orderId + "?driverId=" + driverId;
        restTemplate.getForEntity(url,String.class).getBody();
        return "success";
    }
}
